package com.max.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.max.base.BaseController;
import com.max.common.R;
import com.max.constants.PublicConstants;
import com.max.entity.MettingRoom;
import com.max.service.IMettingRoomService;
import com.max.utils.PublicUtils;
import com.max.utils.SecurityUtils;
import com.max.vo.MettingRoomListVO;
import com.max.vo.TimeSlotVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.List;
import java.util.Objects;

@Api(tags = "会议室开放接口")
@RestController
@RequestMapping("/open/room")
@Validated
public class MettingRoomOpenApi extends BaseController {


    @Autowired
    private IMettingRoomService mettingRoomService;

    @ApiImplicitParams({
            @ApiImplicitParam(value = "会议室名称", name = "roomName", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "会议室编号", name = "roomNum", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "会议室地址", name = "roomAddress", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "会议室状态（0不可用，1可用）", name = "roomState", dataType = "Boolean", paramType = "query"),
            //分页参数
            @ApiImplicitParam(value = "当前页", name = "pageNum", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(value = "每页数量", name = "pageSize", dataType = "Integer", paramType = "query")
    })
    @ApiOperation("查看会议室列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R<List<MettingRoomListVO>> roomList(MettingRoom mettingRoom) {
        startPage();
        LambdaQueryWrapper<MettingRoom> eq = new LambdaQueryWrapper<MettingRoom>()
                .like(StringUtils.isNotBlank(mettingRoom.getRoomName()), MettingRoom::getRoomName, mettingRoom.getRoomName())
                .eq(StringUtils.isNotBlank(mettingRoom.getRoomNum()), MettingRoom::getRoomNum, mettingRoom.getRoomNum())
                .like(StringUtils.isNotBlank(mettingRoom.getRoomAddress()), MettingRoom::getRoomAddress, mettingRoom.getRoomAddress())
                .eq(!Objects.isNull(mettingRoom.getRoomState()), MettingRoom::getRoomState, mettingRoom.getRoomState());
        List<MettingRoom> list = mettingRoomService.list(eq);
        List<MettingRoomListVO> voList = PublicUtils.copyList(list, MettingRoomListVO.class);
        return R.ok(voList);
    }

    @ApiImplicitParam(value = "会议室ID", name = "id", dataType = "Long", paramType = "path", required = true)
    @ApiOperation("查看会议室详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public R<MettingRoomListVO> selectRoomById(@PathVariable("id") Long id) {
        MettingRoom byId = mettingRoomService.getById(id);
        MettingRoomListVO res = new MettingRoomListVO();
        BeanUtils.copyProperties(byId, res);
        return R.ok(res);
    }

    @Autowired
    private SecurityUtils securityUtils;

    @ApiOperation("查看会议室可用时段")
    @RequestMapping(value = "/timeDetail", method = RequestMethod.GET)
    public R<List<TimeSlotVO>> selectRoomTimeDetailById(
            @NotNull(message = "会议室ID不能为空") Long roomId,
            @Pattern(regexp = PublicConstants.datePattern, message = "请输入正确的日期格式") String useDate
    ) {
        List<TimeSlotVO> list = mettingRoomService.selectRoomTimeDetailById(roomId, useDate);
        return R.ok(list);
    }


}
